/**
 * 缓存操作
 * 缓存表单内容
 */
import md5 from "./md5.min.js";
import {
	format
} from "./api.js";

export class Cache {
	#key;
	constructor(arg) {
		this.#key = md5(arg.key || Math.random());
	}
	set Key(key) {
		this.#key = md5(key);
	}
	set time(e) {
		uni.setStorageSync(this.#key + 'time', e);
	}
	get time() {
		return uni.getStorageSync(this.#key + 'time');
	}
	set data(e) {
		if(e){
			this.time = format(new Date());
			uni.setStorageSync(this.#key, e);
		}else{
			this.remove();
		}
	}
	get data() {
		return uni.getStorageSync(this.#key);
	}
	remove() {
		uni.removeStorage({
			key: this.#key
		});
		uni.removeStorage({
			key: this.#key + 'time'
		});
	}
}
export const CACHE = {}
export const cache = arg => {
	const key = md5(arg.key);
	return CACHE[key] = CACHE[key] || new Cache(arg);
}